/// where all the fun stuff happens
/// a lot of forward ideas snuck in here for now just calculates the gravity towards another object

class gravity {

  particle one;
  particle two;

  float gravity;
  float strength;

  float distance;

  gravity() {
  } 

  void set_particle_one(particle one) {
    this.one = one;
  }

  void set_distance(float distance) {
    this.distance = distance;
  }

  void apply_gravity(particle two) {
    this.two = two; 
    apply_gravity();
  }

  void apply_gravity(particle one, particle two) {
    this.one = one;
    this.two = two;
    apply_gravity();
  }

  void apply_gravity() {
    calculate_strength();
    apply_rotation();
  }

  /// dump all saved data
  void reset() {
    one = null;
    two  = null;
    strength = 0;
    gravity = 0;
  }

  void apply_rotation() {
    one.rotation = one.rotation +
      attract(one.rotation,one.x_loc,one.y_loc,
    two.x_loc,two.y_loc,
    strength);
  }


  float calculate_strength() {
    strength = ((distance*distance)*(one.mass+two.mass))*.00000001;
   
    return strength;
  }

  float attract(float o, float x1, float y1, float x2, float y2, float change) {
    float direction_to = direction_to(x1, y1, x2, y2);
    if(direction_to > o) {
      if( direction_to - o < 360 -direction_to + o) {
        return change;
      }
      else {
        return change*-1;
      }
    }
    if(direction_to < o) {
      if(  o - direction_to < 360-o+direction_to ) {
        return change*-1;
      }
      else {
        return change;
      }
    }
    return 0;
  }

  float direction_to(float one_x_loc, float one_y_loc, float two_x_loc, float two_y_loc) {
    float a = degrees(atan2(one_y_loc - two_y_loc, one_x_loc- two_x_loc)) +180 ;
    return a;
  }
}

